package com.xjh.basestudy.designmode.behaviorMode.behaviorMode2_Iterator.p1_different;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XuJianHua
 * @date 2022-09-29 16:54:23
 * @description 遍历书：了解迭代遍历和不迭代遍历的区别
 */
public class Book {

    class Page {
        private int index;

        public Page(int index) {
            this.index = index;
        }

        @Override
        public String toString() {
            return "阅读第" + this.index + "页";
        }
    }

    private List<Page> pages = new ArrayList<>();

    public Book(int pageSize) {
        for (int i = 0; i < pageSize; i++) {
            pages.add(new Page(i + 1));
        }
    }
// 不迭代的方式遍历
    public void readNotFor() {
        System.out.println(pages.get(0));
        System.out.println(pages.get(1));
        System.out.println(pages.get(2));
        System.out.println(pages.get(3));
        System.out.println("......");
        System.out.println(pages.get(99));
    }

    // 迭代的方式遍历
    public void readByFor() {
        for (Page page : pages) {
            System.out.println(page);
        }
    }

}